package com.dbb.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.goldstone.entity.Attachment;

public class FileUtil {
	private static Map<String, File> files = new HashMap<String, File>();
	public static Map<String, File> getFiles(String filePath) {
		File root = new File(filePath);
		if(root.isDirectory()) {
			//System.out.println(root.getAbsolutePath());
			File[] fs = root.listFiles();
			for(File f : fs) {
				getFiles(f.getAbsolutePath());
			}
		} else {
			files.put(root.getAbsolutePath(), root);
		}
		return files;
	}
	
	public static String getFileName() {
		return String.valueOf(Calendar.getInstance().getTimeInMillis());
	}
	
	@Test
	public void testGetFile() {
		Map<String, File> fs = getFiles("C:\\Dropbox"); 
		for(String m : fs.keySet()) {
			System.out.println(m);
		}
	}
	
	public static String copyfile(File from) {
		return copyfile(from, getFileName());
	}
	
	public static String copyfile(File from, String dtFile) {
		String filePath = Content.FILE_PATH + "atta/" + dtFile;
		try {
			File f2 = new File(filePath);
			InputStream in = new FileInputStream(from);
			OutputStream out = new FileOutputStream(f2);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			return filePath;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}
	public static String getFilePath(String contentType, String fileName) {
		return getFilePath(contentType, fileName, null);
	}
	public static String getFilePath(String contentType, String fileName, ImageSizeType sizeType) {
		if(contentType.indexOf("image") != -1) {
			if(ImageSizeType.L.equals(sizeType)) {
				return Content.FILE_PATH + "L/" + fileName + ".jpg";
			} else if (ImageSizeType.S.equals(sizeType)) {
				return Content.FILE_PATH + "S/" + fileName + ".jpg";
			} else if (ImageSizeType.M.equals(sizeType)) {
				return Content.FILE_PATH + "M/" + fileName + ".jpg";
			} else {
				return Content.FILE_PATH + "Logo/" + fileName + ".jpg";
			}
			
		} else {
			return fileName;
		}
		
	}
	
	public static String getFilePath(File file, String contentType) throws Exception {
		String fileName = getFileName();
		if(contentType.indexOf("image") != -1) {
			ImageManager m = new ImageManager(file);
			m.resize(fileName, ImageSizeType.Logo);
			m.resize(fileName, ImageSizeType.S);
			m.resize(fileName, ImageSizeType.M);
			m.resize(fileName, ImageSizeType.L);
			return fileName;
		} else {
			return copyfile(file, fileName);
		}
	}


	public static String getFilePath(Attachment attachment, ImageSizeType imageType) {
		return getFilePath(attachment.getContentType(), attachment.getFilePath(), imageType);
	}
	
	public static String getFilePath(Attachment attachment) {
		return getFilePath(attachment.getContentType(), attachment.getFilePath());
	}

	public static String downLoad(Attachment attachment) {
		if(attachment.getContentType().indexOf("image") != -1) {
			return Content.FILE_PATH + "L/" + attachment.getFilePath() + ".jpg";
		} else {
			return attachment.getFilePath();
		}
	}
}
